<template>
  <div class="login">
    <div class="login-header">
      <div class="header-title">
        <div class="txt">{{ title }}</div>
      </div>
    </div>
    <el-form
      ref="loginRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <el-form-item prop="username" class="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          size="large"
          auto-complete="off"
          placeholder="验证码"
          style="width: 67%"
          @keyup.enter="handleLogin"
        >
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img" />
        </div>
      </el-form-item>

      <el-form-item class="btn">
        <div
          v-loading="loading"
          class="login-btn"
          size="large"
          type="primary"
          element-loading-background="rgba(0, 0, 0, 0.15)"
          @click.prevent="handleLogin"
        >
          <!-- <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span> -->
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2023-2024 孚山驽势 All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script setup>
import { getCodeImg, getConfigKey } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from "@/utils/jsencrypt";
import useUserStore from "@/store/modules/user";
import useSettingsStore from "@/store/modules/settings";
import { computed } from "vue";

const userStore = useUserStore();
const route = useRoute();
const router = useRouter();
const { proxy } = getCurrentInstance();

const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false,
  code: "",
  uuid: "",
});

const loginRules = {
  username: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
  code: [{ required: true, trigger: "change", message: "请输入验证码" }],
};

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(false);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);
// const title = ref("")

watch(
  route,
  (newRoute) => {
    redirect.value = newRoute.query && newRoute.query.redirect;
  },
  { immediate: true }
);

const title = computed(
  () => useSettingsStore().appTitle || "露天矿边坡监测平台"
);

function handleLogin() {
  proxy.$refs.loginRef.validate((valid) => {
    if (valid) {
      loading.value = true;
      // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
      if (loginForm.value.rememberMe) {
        Cookies.set("username", loginForm.value.username, { expires: 30 });
        Cookies.set("password", encrypt(loginForm.value.password), {
          expires: 30,
        });
        Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
      } else {
        // 否则移除
        Cookies.remove("username");
        Cookies.remove("password");
        Cookies.remove("rememberMe");
      }
      // 调用action的登录方法
      userStore
        .login(loginForm.value)
        .then(() => {
          const query = route.query;
          const otherQueryParams = Object.keys(query).reduce((acc, cur) => {
            if (cur !== "redirect") {
              acc[cur] = query[cur];
            }
            return acc;
          }, {});
          router.push({ path: redirect.value || "/", query: otherQueryParams });
        })
        .catch(() => {
          console.log("登录失败");
          loading.value = false;
          // 重新获取验证码
          if (captchaEnabled.value) {
            getCode();
          }
        });
    }
  });
}

function getCode() {
  getCodeImg().then((res) => {
    captchaEnabled.value =
      res.captchaEnabled === undefined ? true : res.captchaEnabled;
    if (captchaEnabled.value) {
      codeUrl.value = "data:image/gif;base64," + res.img;
      loginForm.value.uuid = res.uuid;
    }
  });
}

function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password:
      password === undefined ? loginForm.value.password : decrypt(password),
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  };
}

getCode();
getCookie();
</script>

<style lang="less" scoped>
.login-header {
  position: fixed;
  top: 0;
  width: 100%;
  height: 80px;
  background-image: url("../assets/images/header.png");
  background-size: cover;
}

.header-title {
  display: flex;
  justify-content: center;

  .txt {
    font-family: PangMen;
    color: #fff;
    text-align: justify;
    font-size: 46px;
    font-style: normal;
    font-weight: 500;
    line-height: 80px;
    background: linear-gradient(90deg, #ffffff 0%, #90deff 100%);
    -webkit-background-clip: text;
    /*将设置的背景颜色限制在文字中*/
    -webkit-text-fill-color: transparent;
    /*给文字设置成透明*/
  }
}

.login {
  height: 100%;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  background-image: url("../assets/images/login-bg.png");
  background-size: cover;
}

.title {
  font-size: 28px;
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
}

.login-form {
  border-radius: 6px;
  background-image: url("../assets/images/login-form.png");
  background-size: 100% 100%;
  width: 400px;
  padding: 2rem 0.25rem 0.05rem 0.25rem;
  margin-right: 2rem;

  .el-input {
    height: 40px;

    input {
      height: 40px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 0px;
  }

  .username {
    margin-top: 70px;
  }

  .btn {
    width: 100%;
    margin: 110px 0;
  }

  .login-btn {
    width: 70%;
    height: 50px;
    background-image: url("../assets/images/login-btn.png");
    background-size: cover;
    cursor: pointer;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 40px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 40px;
  padding-left: 20px;
}

:deep(.el-form-item__content) {
  justify-content: center;
}

:deep(.el-input__wrapper) {
  background-color: transparent;
  border: 1px solid #3da0fa;
  box-shadow: none;
  .el-input__inner {
    color: #bbcfff;
  }
}
</style>
